import tkinter as tk
from tkinter import filedialog


def open_file():
    file_path = filedialog.askopenfilename(title='选择文件',
                                           filetypes=[('Python 文件', '*.py *.pyw'), ('所有文件', '*')])
    excel_entry.insert(0, file_path)


# 创建主窗口
window = tk.Tk()
window.title('my window')
# 宽 高
window.geometry('800x600')

# 按钮示例：点击插入新数据
button = tk.Button(window, text="添加新行", command=lambda: append_text("新行内容"))
# button = tk.Button(window, text="添加新行", command=lambda: append_text("新行内容", button))
button.pack()

# label
excel_lable = tk.Label(window, text='Excel 路径：', font=('Arial', 17))
excel_lable.place(x=20, y=100)
# excel 路径
# excel_entry = tk.Entry(window, show=None, width=50, justify='right')
excel_entry = tk.Entry(window, width=42, font=('Arial', 17))
excel_entry.insert(0, 'aaa')
excel_entry.place(x=160, y=100)
print(excel_entry.get())
# 打开按钮
open_button = tk.Button(window, text='打开文件', command=open_file)
open_button.place(x=700, y=100)

# 输出框
output_text = tk.Text(window, height=19, width=70, wrap=tk.WORD, state='disabled', font=('Arial', 12))
output_text.place(x=80, y=200)

# 输出框插入数据
def append_text(content):
    button.config(state='disabled')
    # 临时启用编辑状态
    output_text.config(state='normal')
    # 插入内容并换行
    output_text.insert('end', content + '\n')
    # 恢复只读状态
    output_text.config(state='disabled')
    # 滚动到底部
    output_text.see('end')
    # button.config(state='disabled')

# 分多次插入数据的示例
append_text("第一条消息")
append_text("第二条消息")
append_text("第三条消息")

# 运行主循环
window.mainloop()
